<template>
  <div :key="vueRest" class="addBrand-container">
    <div :key="restKey" class="container">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        :inline="true"
        label-width="180px"
        class="demo-ruleForm"
      >
        <el-form-item label="材料描述:">
          <el-input
            v-model="ruleForm.description"
            type="textarea"
            :rows="3"
            maxlength="200"
            placeholder="材料描述，最长200字"
          />
        </el-form-item>
        <el-form-item label="菜品图片:" prop="image">
          <image-upload :prop-image-url="imageUrl" @imageChange="imageChange">
            图片大小不超过2M<br />仅能上传 PNG JPEG JPG类型图片<br />建议上传200*200或300*300尺寸的图片
          </image-upload>
        </el-form-item>

        <div class="subBox address">
          <el-button @click="() => $router.back()">取消</el-button>
          <el-button
            type="primary"
            :class="{ continue: actionType === 'add' }"
            @click="submitForm('ruleForm')"
            >保存</el-button
          >
          <el-button
            v-if="actionType == 'add'"
            type="primary"
            @click="submitForm('ruleForm', 'goAnd')"
            >保存并继续添加</el-button
          >
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import HeadLable from '@/components/HeadLable/index.vue'
import ImageUpload from '@/components/ImgUpload/index.vue'
// import AddDish from './components/AddDish.vue'
import { queryCarouselById, editCarousel, addCarousel } from '@/api/carousel.ts'

@Component({
  name: 'addShop',
  components: {
    HeadLable,
    // AddDish,
    ImageUpload,
  },
})
export default class extends Vue {
  private restKey: number = 0
  private imageUrl: string = ''
  private actionType: string = ''

  // private headers = {
  //   token: getToken()
  // }
  private ruleForm = {
    image: '',
    description: '',
  }

  get rules() {
    return {
      image: {
        required: true,
        message: '菜品图片不能为空',
      },
    }
  }

  created() {
    this.actionType = this.$route.query.id ? 'edit' : 'add'
    if (this.actionType == 'edit') {
      this.init()
    }
  }
  private async init() {
    queryCarouselById(this.$route.query.id).then((res) => {
      if (res && res.data && res.data.code === 1) {
        this.ruleForm = res.data.data
        // this.imageUrl = `http://172.17.2.120:8080/common/download?name=${res.data.data.image}`
        this.imageUrl = res.data.data.image
      } else {
        this.$message.error(res.data.msg)
      }
    })
  }

  public submitForm(formName: any, st: any) {
    if (!this.ruleForm.image) return this.$message.error('材料图片不能为空')
    let prams = { ...this.ruleForm } as any
    if (this.actionType == 'add') {
      delete prams.id
      addCarousel(prams)
        .then((res) => {
          if (res && res.data && res.data.code === 1) {
            this.$message.success('添加成功！')
            if (!st) {
              this.$router.push({ path: '/carousel' })
            } else {
              ;(this as any).$refs.ruleForm.resetFields()
              this.ruleForm = {
                image: '',
                description: '',
              } as any
              this.imageUrl = ''
            }
          } else {
            this.$message.error(res.data.msg)
          }
        })
        .catch((err) => {
          this.$message.error('请求出错了：' + err.message)
        })
    } else {
      delete prams.updateTime
      editCarousel(prams)
        .then((res) => {
          if (res.data.code === 1) {
            this.$message.success('材料修改成功！')
            this.$router.push({ path: '/carousel' })
          } else {
            // this.$message.error(res.data.desc || res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error('请求出错了：' + err.message)
        })
    }
  }

  imageChange(value: any) {
    this.ruleForm.image = value
  }
}
</script>
<style>
.avatar-uploader .el-icon-plus:after {
  position: absolute;
  display: inline-block;
  content: ' ' !important;
  left: calc(50% - 20px);
  top: calc(50% - 40px);
  width: 40px;
  height: 40px;
  background: url('./../../assets/icons/icon_upload@2x.png') center center
    no-repeat;
  background-size: 20px;
}
</style>
<style lang="scss">
// .el-form-item__error {
//   top: 90%;
// }
.addBrand-container {
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 30px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #83a2ff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 200px;
    height: 160px;
    line-height: 160px;
    text-align: center;
  }

  .avatar {
    width: 200px;
    height: 160px;
    display: block;
  }

  // .el-form--inline .el-form-item__content {
  //   width: 293px;
  // }

  .el-input {
    width: 293px;
  }

  .address {
    .el-form-item__content {
      width: 777px !important;
    }
  }

  .el-input__prefix {
    top: 2px;
  }

  .addDish {
    .el-input {
      width: 130px;
    }

    .el-input-number__increase {
      border-left: solid 1px #fbe396;
      background: #fffbf0;
    }

    .el-input-number__decrease {
      border-right: solid 1px #fbe396;
      background: #fffbf0;
    }

    input {
      border: 1px solid #fbe396;
    }

    .table {
      border: solid 1px #ebeef5;
      border-radius: 3px;

      th {
        padding: 5px 0;
      }

      td {
        padding: 7px 0;
      }
    }
  }

  .addDishList {
    .seachDish {
      position: absolute;
      top: 12px;
      right: 20px;
    }

    .el-dialog__footer {
      padding-top: 27px;
    }

    .el-dialog__body {
      padding: 0;
      border-bottom: solid 1px #efefef;
    }

    .seachDish {
      .el-input__inner {
        height: 40px;
        line-height: 40px;
      }
    }
  }
}
</style>
<style lang="scss" scoped>
.addBrand {
  &-container {
    margin: 30px;
    border-radius: 30px;
    .container {
      position: relative;
      z-index: 1;
      background: #fff;
      padding: 30px;
      border-radius: 30px;
      min-height: 500px;

      .subBox {
        padding-top: 30px;
        text-align: center;
        border-top: solid 1px $gray-5;
      }

      .el-input {
        width: 350px;
      }

      .addDish {
        width: 777px;

        .addBut {
          background: #83a2ff;
          display: inline-block;
          padding: 0px 20px;
          border-radius: 3px;
          line-height: 40px;
          cursor: pointer;
          border-radius: 30px;
          color: #333333;
          font-weight: 500;
        }

        .content {
          background: #fafafb;
          padding: 20px;
          border: solid 1px #d8dde3;
          border-radius: 30px;
        }
      }
    }
  }
}
</style>
